package com.bootdo.redis;
import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.common.utils.Hog;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
 /**
　　* @author 郝江波
　　* @date 2019/9/3 11:51
　　*/
public class HjbRedisCatch implements Cache {
    private final ReadWriteLock readWriteLock = new DummyReadWriteLock();

    private String id;

    private static volatile RedisTemplate redisTemplate;

    public HjbRedisCatch(String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        } else {
            this.id = id;
        }
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        if(null == redisTemplate) {
            getSpringRedisTemplate(redisTemplate);
        }
        if(null != redisTemplate){
            //Hog.i("存缓存 k =  {} v = {}" ,key,value);
            HashOperations  hashOperations  =  redisTemplate.opsForHash();
            hashOperations.put(this.id, key.toString(), value);
            //expire(this.id,60*60*24);
        }
    }

    @Override
    public Object getObject(Object key) {
        //Hog.i("走缓存 = {}",key);
        if(null == redisTemplate) {
            getSpringRedisTemplate(redisTemplate);
        }
        if(null != redisTemplate){
           Object object =   redisTemplate.opsForHash().get(this.id, key.toString());
            return object ;
        }else{
            return  null;
        }
    }

    @Override
    public Object removeObject(Object key) {
        if(null == redisTemplate) {
            getSpringRedisTemplate(redisTemplate);
        }
        if(null != redisTemplate){
            return redisTemplate.opsForHash().delete(this.id, key);
        }else{
            return  null;
        }
    }

    @Override
    public void clear() {
        if(null == redisTemplate) {
            getSpringRedisTemplate(redisTemplate);
        }
        if(null != redisTemplate){
            redisTemplate.delete(this.id);
        }
    }

    @Override
    public int getSize() {
        if(null == redisTemplate) {
            getSpringRedisTemplate(redisTemplate);
        }
        if(null != redisTemplate){
            return redisTemplate.opsForHash().size(this.id).intValue();
        }else{
            return  0 ;
        }
    }


    public static  void getSpringRedisTemplate(RedisTemplate p0){
        //"redisTemplate",
        redisTemplate = ApplicationContextRegister.getApplicationContext().getBean("redisTemplate",RedisTemplate.class);
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }
    private boolean expire(String key, long time) {
         try {
             if (time > 0) {
                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
             }
             return true;
         } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
     }

}
